package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.app.domain.AppUserAgreement;
import com.study.app.mapper.AppUserAgreementMapper;
import com.study.app.service.IAppUserAgreementService;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.app.domain.bo.AppUserAgreementBo;
import com.study.app.domain.vo.AppUserAgreementVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * App用户协议隐私条款维护Service业务层处理
 *
 * @author study
 * @date 2023-09-25
 */
@RequiredArgsConstructor
@Service
public class AppUserAgreementServiceImpl implements IAppUserAgreementService {

    private final AppUserAgreementMapper baseMapper;

    /**
     * 查询App用户协议隐私条款维护
     */
    @Override
    public AppUserAgreementVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询App用户协议隐私条款维护列表
     */
    @Override
    public TableDataInfo<AppUserAgreementVo> queryPageList(AppUserAgreementBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserAgreement> lqw = buildQueryWrapper(bo);
        Page<AppUserAgreementVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询App用户协议隐私条款维护列表
     */
    @Override
    public List<AppUserAgreementVo> queryList(AppUserAgreementBo bo) {
        LambdaQueryWrapper<AppUserAgreement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserAgreement> buildQueryWrapper(AppUserAgreementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserAgreement> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVersionOn()), AppUserAgreement::getVersionOn, bo.getVersionOn());
        lqw.like(StringUtils.isNotBlank(bo.getVersionName()), AppUserAgreement::getVersionName, bo.getVersionName());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionInfo()), AppUserAgreement::getVersionInfo, bo.getVersionInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionContent()), AppUserAgreement::getVersionContent, bo.getVersionContent());
        lqw.eq(bo.getVersionStatus() != null, AppUserAgreement::getVersionStatus, bo.getVersionStatus());
        lqw.eq(bo.getVersionType() != null, AppUserAgreement::getVersionType, bo.getVersionType());
        return lqw;
    }

    /**
     * 新增App用户协议隐私条款维护
     */
    @Override
    public Boolean insertByBo(AppUserAgreementBo bo) {
        AppUserAgreement add = BeanUtil.toBean(bo, AppUserAgreement.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改App用户协议隐私条款维护
     */
    @Override
    public Boolean updateByBo(AppUserAgreementBo bo) {
        AppUserAgreement update = BeanUtil.toBean(bo, AppUserAgreement.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserAgreement entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除App用户协议隐私条款维护
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public AppUserAgreementVo getLatestInformation(Integer type) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<AppUserAgreement>()
                .eq(AppUserAgreement::getVersionType, type)
                .eq(AppUserAgreement::getVersionStatus, 2));
    }
}
